package com.itheima.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.pojo.Book;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;


@SpringBootTest
public class BookDaoTestCase {

    @Autowired
    private BookDao bookDao;
//按id查询
    @Test
    public void testGetById() {
        System.out.println(bookDao.selectById(1));
    }
//添加数据
    @Test
    public void testSave(){
        Book book = new Book();
        book.setType("恐吓");
        book.setName("夺魂");
        book.setDescription("很吓人");
        bookDao.insert(book);
    }
    //修改数据
    @Test
    public void testUpdate(){
        Book book = new Book();
        book.setId(10);
        book.setType("恐吓");
        book.setName("夺魂2");
        book.setDescription("一点不吓人");
        bookDao.updateById(book);
    }
    //修改数据
    @Test
    public void testDelete(){
        bookDao.deleteById(10);

    }
    //查询全部
    @Test
    public void testGetAll(){

        System.out.println(bookDao.selectList(null));

    }
    //分页查询
    @Test
    public void testGetPage(int currentPage,int pageSize){
        IPage page = new Page(1,5);
        bookDao.selectPage(page,null);
        //数据list集合
        System.out.println(page.getRecords());
        //能有几页
        System.out.println(page.getPages());//2
        //当前页
        System.out.println(page.getCurrent());//1
        //每页显示的条数
        System.out.println(page.getSize());//5
        //总条数
        System.out.println(page.getTotal());//10


    }
    //按条件查询查询
    @Test
    public void testGetBy(){
        QueryWrapper qw = new QueryWrapper();
        qw.like("name","夺");
        bookDao.selectList(qw);
    }
    @Test
    public void testGetBy1(){
        LambdaQueryWrapper<Book> lqw = new LambdaQueryWrapper<Book>();
        String name = null;
        //lqw.like(Book::getName, name);
        lqw.like(name!=null,Book::getName,name);
        bookDao.selectList(lqw);
    }
}
